package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblCourseMySql;
import com.huabo.system.mysql.entity.TblVideoTypeMySql;
import com.huabo.system.mysql.mapper.TblCourseMySqlMapper;
import com.huabo.system.mysql.mapper.TblVideoTypeMySqlMapper;
import com.huabo.system.oracle.entity.TblCourse;
import com.huabo.system.oracle.entity.TblVideoType;
import com.huabo.system.oracle.mapper.TblCourseMapper;
import com.huabo.system.oracle.mapper.TblVideoTypeMapper;
import com.huabo.system.service.TblCourseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TblCourseServiceImpl implements TblCourseService {
    @Resource
    private TblCourseMapper courseMapper;

    @Resource
    private TblVideoTypeMapper tblVideoTypeMapper;

    @Resource
    private TblCourseMySqlMapper tblCourseMySqlMapper;

    @Resource
    private TblVideoTypeMySqlMapper tblVideoTypeMySqlMapper;


    @Override
    public void savetblCourse(TblCourse tblCourse) {
        courseMapper.savetblCourse(tblCourse);
    }

    @Override
    public void saveMySqltblCourse(TblCourseMySql tblCourseMySql) {
        tblCourseMySqlMapper.savetblCourse(tblCourseMySql);
    }

    @Override
    public void updatetblCourse(TblCourse tblCourse) {
        courseMapper.updatetblCourse(tblCourse);
    }

    @Override
    public void updateMySqltblCourse(TblCourseMySql tblCourseMySql) {
        tblCourseMySqlMapper.updatetblCourse(tblCourseMySql);
    }

    @Override
    public TblCourse geTblCourse(String id) {
        return courseMapper.selectByCourseId(id);
    }

    @Override
    public TblCourseMySql geTblCourseMySql(String id) {
        return tblCourseMySqlMapper.selectByCourseId(id);
    }


    @Override
    public List<TblCourse> findByFatherid(String pid) {
        return courseMapper.listBypid(pid);
    }

    @Override
    public List<TblCourseMySql> findByMySqlFatherid(String pid) {
        return tblCourseMySqlMapper.listBypid(pid);
    }

    @Override
    public Map<String, Object> courseList(Integer pageNumber, Integer pageSize, String token, String staffId, String coursename1, String coursetype1) {


        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            if (token == null) {
                resultMap.put("code", "0");
                resultMap.put("msg", "用户已失效！");
                return resultMap;
            }
            TblStaffUtil staff = DealUserToken.parseUserToken(token);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();


            if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {

                List<TblVideoType> tblVideoTypes = tblVideoTypeMapper.findByorgid(staff.getCurrentOrg().getOrgid());
                PageInfo<TblCourse> pageInfo = new PageInfo<TblCourse>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(courseMapper.selectListByPageInfo(pageInfo, orgid, coursename1, coursetype1));
                pageInfo.setTotalRecord(courseMapper.selectCountByPageInfo(pageInfo, orgid, coursename1, coursetype1));
                Map<String, Object> dataMap = new HashMap<String, Object>(0);
                dataMap.put("pageInfo", pageInfo);
                dataMap.put("typelist", tblVideoTypes);
                dataMap.put("coursename1", coursename1);
                dataMap.put("coursetype1", coursetype1);
                resultMap.put("data", dataMap);

            } else {

                List<TblVideoTypeMySql> tblVideoTypes = tblVideoTypeMySqlMapper.findByorgid(staff.getCurrentOrg().getOrgid());
                PageInfo<TblCourseMySql> pageInfo = new PageInfo<TblCourseMySql>();
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setPageSize(pageSize);
                pageInfo.setTlist(tblCourseMySqlMapper.selectListByPageInfo(pageInfo, orgid, coursename1, coursetype1));
                pageInfo.setTotalRecord(tblCourseMySqlMapper.selectCountByPageInfo(pageInfo, orgid, coursename1, coursetype1));
                Map<String, Object> dataMap = new HashMap<String, Object>(0);
                dataMap.put("pageInfo", pageInfo);
                dataMap.put("typelist", tblVideoTypes);
                dataMap.put("coursename1", coursename1);
                dataMap.put("coursetype1", coursetype1);
                resultMap.put("data", dataMap);

            }

            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> deletetblCourse(String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        courseMapper.deleteByCourseId(id);
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            courseMapper.deleteByCourseId(id);
        } else {
            tblCourseMySqlMapper.deleteByCourseId(id);
        }


        resultMap.put("code", "1");
        resultMap.put("msg", "删除成功");
        return resultMap;

    }
}
